//
//  SecitonView.m
//  Youhuiju
//
//  Created by 余尚飞 on 16/8/9.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "SecitonView.h"

#import "SectionLabelCollectionViewCell.h"


@interface SecitonView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>




@property(nonatomic,assign)NSInteger flag;



//占位数据
@property(nonatomic,strong)NSArray *dataArray;
@end

@implementation SecitonView

static id _sharedInstance = nil;
static NSString *SectionLabelCollectionViewCellIden = @"SectionLabelCollectionViewCell";

+(instancetype)getInstance
{
    static dispatch_once_t oncePredicate;
    
    dispatch_once(&oncePredicate, ^{
        _sharedInstance = [[self alloc] init];
    });
    
    return _sharedInstance;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    _sharedInstance = [super initWithFrame:frame];
    
    [self creatView];
    
    return _sharedInstance;
}

-(void)creatView
{
    self.num = 1;
    self.flag = 0;
    self.dataArray = @[@"爆款直降",@"精品美妆",@"洗护沐浴",@"品质家具",@"澳洲生鲜",@"品质母婴",@"以父之名"];
    
    
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button.backgroundColor = [UIColor colorWithHexString:@"ff5f7b"];
    [self.button setBackgroundImage:LoadImage(@"xia") forState:UIControlStateNormal];

    [self addSubview:self.button];
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-20 * FitW);
        make.bottom.equalTo(self.mas_bottom).offset(-5 * FitH);
        make.size.mas_equalTo(CGSizeMake(30 * FitW, 30 * FitH));
    }];
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    _flowLayout = [[UICollectionViewFlowLayout alloc]init];
  _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _flowLayout.minimumInteritemSpacing = 0 * FitH;
    _flowLayout.minimumLineSpacing = 10 * FitW;
    
    self.collecitonView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_flowLayout];
//    self.collecitonView.backgroundColor = [UIColor colorWithHexString:@"fd743e"];
    self.collecitonView.backgroundColor = [UIColor colorWithHexString:@"ff5f7b"];
//    self.collecitonView.backgroundColor = [UIColor grayColor];
    self.collecitonView.showsHorizontalScrollIndicator = NO;
    self.collecitonView.delegate = self;
    self.collecitonView.dataSource = self;
    [self addSubview:self.collecitonView];
    [self.collecitonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.button.mas_left);
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom);
    }];
    [self.collecitonView registerNib:[UINib nibWithNibName:@"SectionLabelCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:SectionLabelCollectionViewCellIden];
    
 
}

#pragma mark ------ collectionView delegate


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}



-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SectionLabelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:SectionLabelCollectionViewCellIden forIndexPath:indexPath];
    
    cell.titleLabel.text = self.dataArray[indexPath.row];
    cell.dataString = self.dataArray[indexPath.row];
    

    DLog(@"self.flag:%zd-----self.num:%zd----------",self.flag,self.num);

    DLog(@"%zd",self.num - 1);

        if ( indexPath.row == self.num - 1  )
        {
            cell.backgroundColor = [UIColor whiteColor];
            cell.titleLabel.textColor = [UIColor colorWithHexString:@"ff5f7d"];
        }else{
            cell.backgroundColor = [UIColor clearColor];
            cell.titleLabel.textColor = [UIColor whiteColor];
        }

    
    cell.layer.cornerRadius = 3 * FitH;



    return cell;

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    DLog(@"%.2f---%.2f",24 * FitH,10 * FitH);

    return CGSizeMake(70 * FitW, 24 * FitH);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10 * FitH, 12 * FitW, 10 * FitH, 10 * FitW);
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
        self.num = indexPath.row + 1;

    
    DLog(@"%zd-----%@",indexPath.row,self.dataArray[indexPath.row]);
    
//    self.num = indexPath.row;
    DLog(@"%zd-----%@",self.num,self.dataArray[indexPath.row]);
    
    if (self.num - 1 >= 1 && self.num - 1 < self.dataArray.count - 1)
    {
        NSInteger collectionWid = 0;
        for (NSInteger j = 2; j < self.num + 1; j ++)
        {
            collectionWid += [self WidthWithString:self.dataArray[j] fontSize:13 * FitH height:24 * FitH] + 9 * FitW;
            DLog(@"collWid1:---------%zd--------%zd",collectionWid,[self WidthWithString:self.dataArray[j] fontSize:13 * FitH height:24 * FitH]);
        }
        if (self.flowLayout.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
            
            [self.collecitonView setContentOffset:CGPointMake(collectionWid * FitW, 0) animated:YES];
            DLog(@"collWid2:------------%zd---------%f",collectionWid,self.collecitonView.contentOffset.x);
        }
        
        
    }
    [self.sectionDelegate changeTableViewContentOffset:self.num ];
    
    [self.collecitonView reloadData];//不能用这个，，直接刷新tableView 的 cell
    
}

//传值联动
-(void)setNum:(NSInteger)num
{
    DLog(@"--------self.num:%zd",num);
    if (_num != num)
    {
        _num = num;

        if (num - 1 >= 1 && num - 1 < self.dataArray.count - 1)
        {
            NSInteger collectionWid = 0;
            for (NSInteger j = 2; j < num + 1; j ++)
            {
                collectionWid += [self WidthWithString:self.dataArray[j] fontSize:13 * FitH height:24 * FitH] + 9 * FitW;
            }
            if (self.flowLayout.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
                
                [self.collecitonView setContentOffset:CGPointMake(collectionWid * FitW, 0) animated:YES];
            }
        }else if (num == 1)
        {
            [self.collecitonView setContentOffset:CGPointMake(0 * FitW, 0) animated:YES];
        }
        
        [self.collecitonView reloadData];//不能用这个，，直接刷新tableView 的 cell
        
    }
 
}


-(CGFloat)WidthWithString:(NSString *)string fontSize:(CGFloat)fontSize height:(CGFloat)height;
{
    NSDictionary *attrs = @{NSFontAttributeName:[UIFont systemFontOfSize:13 * FitH]};
    return  [string boundingRectWithSize:CGSizeMake(0, 24 * FitH) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrs context:nil].size.width;
}


-(void)buttonAction:(UIButton *)sender
{
    if (self.buttonClickedHandler)
    {
        self.buttonClickedHandler();
    }
}


@end
